package jmetal.qualityIndicator.calculating;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;

import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.qualityIndicator.fastHypervolume.FastHypervolume;
import jmetal.qualityIndicator.util.FileRoW;
import jmetal.util.MathUtil;

public class ComputeHv {
	public static void main(String[] args) throws IOException {
		int objNum = 6;
		int maxSize = 1000;
		
		String root = "F:/sevn/workspace/data/GPLS-D/exp_results/";
		Solution referencePoint = new Solution(objNum);
		for (int i = 0; i < objNum; i++) {
			referencePoint.setObjective(i, -20000);
		}
		
		String problemName = "knapsack750-6-4D";
		
		double[] hvs = new double[30];
		OutputStream out = new FileOutputStream(new File(root+"gplsd-many-new/"+problemName+"/hv.dat"));
		
		double size = 0;
		for (int i = 0;i<30;i++){
			String path = root + "gplsd-many-new/"+problemName+"/FUN"+i+".tsv";
			SolutionSet solutionSet = new FileRoW().getPopulationFromFile(path, maxSize);
			size += solutionSet.size();
			SolutionSet contributionSolutions = new SolutionSet(solutionSet.size());
			for (int idx = 0; idx < solutionSet.size(); idx++) {
				Solution solution = solutionSet.get(idx);
				boolean flag = true;
				for (int j = 0; j < solution.getNumberOfObjectives(); j++) {
					if (solution.getObjective(j) > referencePoint.getObjective(j)) {
						flag = false;
						break;
					}
				}
				if (flag) {
					contributionSolutions.add(solution);
				}
			}
			double hvValue = new FastHypervolume().computeHypervolume(contributionSolutions, referencePoint);
			out.write(("run"+i+":\t"+hvValue+"\n").getBytes());
			hvs[i] = hvValue;
			System.out.println("run"+i+":\t"+hvValue);
		}
		String aveStr = "ave:\t"+MathUtil.getAvarage(hvs)+"\n";
		System.out.print(aveStr);
		out.write(aveStr.getBytes());
		String stdStr = "std:\t"+MathUtil.getStd(hvs)+"\n";
		System.out.print(stdStr);
		out.write(stdStr.getBytes());
		DecimalFormat df = new DecimalFormat("0.0");
		System.out.println("size:\t"+df.format(size/30.0)+"\n");
		out.write(("size:\t"+df.format(size/30.0)+"\n").getBytes());
		out.close();
	}
}